package com.heima.search.interceptor;

import cn.hutool.core.util.StrUtil;
import com.heima.model.user.pojo.ApUser;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.AppThreadLocalUtil;
import com.heima.utils.common.WmThreadLocalUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @projectName: heima-leadnews
 * @package: com.heima.wemedia.interceptor
 * @className: TokenInterceptor
 * @author: 躺着认真玩
 * @description: Token拦截器
 * @date: 2025/5/19 5:35
 * @version: 1.0
 */
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    //     获取请求头中的数据
        String userId = request.getHeader("userId");
        if (!StrUtil.isBlank(userId)) {

            // threadlocal中存储数据
            ApUser apUser = new ApUser();
            apUser.setId(Integer.valueOf(userId));
            AppThreadLocalUtil.setUser(apUser);
        }
        //     放行
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        WmThreadLocalUtil.clear();
    }
}
